package jee.boot.common.config;

import jee.boot.common.basic.BaseConfig;
import jee.boot.common.properties.ProjectConfig;
import jee.boot.common.remote.RemoteEndpoint;
import jee.boot.common.standard.AccountHandler;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.common.standard.DocumentStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 认证中心默认的接口配置类,可以通过覆盖这些类来实现自定义的功能
 */
@Configuration
public class BasicBeanConfig {
    @Bean
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    /**
     * 配置默认的查询登录账户接口实现类
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(AccountHandler.class)
    public AccountHandler accountHandler(@Autowired ProjectConfig projectConfig) {
        DefaultAccountHandlerImpl handler = new DefaultAccountHandlerImpl(projectConfig);
        return handler;
    }

    /**
     * 配置默认的查询登录用户授权接口实现类
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityHandler.class)
    public AuthorityHandler authorityHandler(@Autowired CacheManager cacheManager, @Autowired ProjectConfig projectConfig, @Autowired AccountHandler accountHandler, @Autowired RemoteEndpoint remoteEndpoint) {
        DefaultAuthorityHandlerImpl handler = new DefaultAuthorityHandlerImpl(cacheManager, projectConfig,accountHandler,remoteEndpoint);
        return handler;
    }

    /**
     * 如果需要文件存储,需要覆盖整个配置
     * @param baseConfig
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(DocumentStorageClient.class)
    public DocumentStorageClient fileService(@Autowired BaseConfig baseConfig) {
        DocumentStorageClient fileStore = new DocumentStorageClient() {
            @Override
            public <T> T uploadFromMultipartFile(MultipartFile file) {
                return null;
            }

            @Override
            public <T> T uploadFromInputStream(InputStream inputStream, String fileName) {
                return null;
            }

            @Override
            public <T> T uploadFromFile(File file) {
                return null;
            }

            @Override
            public File downloadToFile(String attachmentId) throws FileNotFoundException {
                return null;
            }

            @Override
            public OutputStream downloadToOutputStream(String attachmentId) throws FileNotFoundException {
                return null;
            }

            @Override
            public void deleteFile(String attachmentId) throws FileNotFoundException {

            }
        };
        return fileStore;
    }

}
